package com.taotao.rest.service.impl;

import com.taotao.common.utils.JsonUtils;
import com.taotao.mapper.TbContentMapper;
import com.taotao.pojo.TbContent;
import com.taotao.pojo.TbContentExample;
import com.taotao.pojo.TbItemExample;
import com.taotao.rest.dao.JedisClient;
import com.taotao.rest.service.ContentService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package com.taotao.rest.service.impl
 * @Description:
 * @Author elwyn
 * @Date 2017/9/4 22:17
 * @Email elonyong@163.com
 */
@Service
public class ContentServiceImpl implements ContentService {
    @Autowired
    private TbContentMapper tbContentMapper;

    @Autowired
    JedisClient jedisClient;

    @Value("${INDEX_CONTENT_REDIS_KEY}")
    private String INDEX_CONTENT_REDIS_KEY;

    @Override
    public List<TbContent> getContentList(long contentCid) {

        try {
            String hget = jedisClient.hget(INDEX_CONTENT_REDIS_KEY, String.valueOf(contentCid));
            if (StringUtils.isNotBlank(hget)) {
                List<TbContent> tbContents = JsonUtils.jsonToList(hget, TbContent.class);
                return tbContents;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        TbContentExample tbContentExample = new TbContentExample();
        TbContentExample.Criteria criteria = tbContentExample.createCriteria();
        criteria.andCategoryIdEqualTo(contentCid);

        List<TbContent> tbContents = tbContentMapper.selectByExample(tbContentExample);

        try {
            String s = JsonUtils.objectToJson(tbContents);
            jedisClient.hset(INDEX_CONTENT_REDIS_KEY, String.valueOf(contentCid), s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tbContents;
    }
}
